package com.CmJava.service.imp;

import com.CmJava.entity.myinfo.Description;
import com.CmJava.entity.myinfo.MyInfo;
import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.User;
import com.CmJava.entity.visitor.Visitor;
import com.CmJava.mapper.DescriptionMapper;
import com.CmJava.mapper.MyInfoMapper;
import com.CmJava.mapper.UserMapper;
import com.CmJava.mapper.VisitorMapper;
import com.CmJava.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.CipherSpi;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class AdminServiceImp implements AdminService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MyInfoMapper myInfoMapper;

    @Autowired
    private DescriptionMapper descriptionMapper;

    @Autowired
    private VisitorMapper visitorMapper;

    @Override
    public ResultEntity adminLogin(String adminAccount, String adminPassword) {
        Wrapper<User> userWrapper = new QueryWrapper<>();
        ((QueryWrapper<User>) userWrapper).eq("user_account", adminAccount)
                .eq("user_password", adminPassword);

        User user = userMapper.selectOne(userWrapper);
        if (user == null) {
            return ResultEntity.failureWithoutMessage();
        }
        return ResultEntity.successWithMessage(user);
    }

    @Override
    public ResultEntity getMyInfo(HttpServletRequest request) {
        MyInfo myInfo = myInfoMapper.selectById("1");

        // 访客记录
        String visitorIp = request.getRemoteAddr();
        if (visitorIp.equals("127.0.0.1")){
            visitorIp="博主";
        }
        String recentVisitTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Visitor visitor = (Visitor) request.getSession().getAttribute("visitor");
        if (visitor == null) {
            QueryWrapper<Visitor> visitorQueryWrapper0 = new QueryWrapper<>();
            visitorQueryWrapper0.eq("visitorIp",visitorIp);
            List<Visitor> visitors = visitorMapper.selectList(visitorQueryWrapper0);
            if (visitors==null || visitors.size()==0) {
                if (visitorIp.equals("127.0.0.1")) {
                    visitorIp = "博主";
                }


                QueryWrapper<Visitor> visitorQueryWrapper = new QueryWrapper<>();
                visitorQueryWrapper.select("*");
                int visitorNum = visitorMapper.selectCount(visitorQueryWrapper);
                Visitor visitor1 = new Visitor(visitorNum + 1, visitorIp, recentVisitTime, "--", 0);
                visitorMapper.insert(visitor1);

                request.getSession().setAttribute("visitor",visitor1);
            }else {
                Visitor visitor1 = visitors.get(0);
                visitor1.setRecentVisitTime(recentVisitTime);
                request.getSession().setAttribute("visitor",visitor1);
                visitorMapper.updateById(visitor1);
            }
        }else {
            visitor.setRecentVisitTime(recentVisitTime);
            request.getSession().setAttribute("visitor",visitor);
            visitorMapper.updateById(visitor);
        }


        if (myInfo != null) {
            return ResultEntity.successWithMessage(myInfo);
        }

        return ResultEntity.failureWithoutMessage();
    }

    @Override
    public ResultEntity getDescription() {
        Description description = descriptionMapper.selectById("1");
        System.out.println(description);
        if (description != null) {
            System.out.println(22);
            return ResultEntity.successWithMessage(description);
        }
        System.out.println(11);
        return ResultEntity.failureWithoutMessage();
    }

    @Override
    public ResultEntity updateMyInFo(MyInfo myInfo) {
        int i = myInfoMapper.updateById(myInfo);
        if (i != 1) {
            return ResultEntity.failureWithoutMessage();
        }
        return ResultEntity.successWithoutMessage();
    }

    @Override
    public ResultEntity updateDescription(Description description1) {
        int i = descriptionMapper.updateById(description1);
        if (i != 1) {
            return ResultEntity.failureWithoutMessage();
        }
        return ResultEntity.successWithoutMessage();
    }
}
